#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class ActionListProvider : CY.GFive.Core.Providers.DALProvider.IActionListProvider
	{
		#region Sql
		private static readonly string SqlSelectActionList = "SELECT [Id], [ActionName] FROM [ActionList] WHERE  [Id]=@Id";

		private static readonly string SqlInsertActionList = "INSERT INTO [ActionList]([ActionName]) values(@ActionName);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateActionList = "UPDATE [ActionList] SET [ActionName] = @ActionName WHERE  [Id]=@Id";

		private static readonly string SqlDeleteActionList = "DELETE [ActionList] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllActionList = "SELECT [Id], [ActionName] FROM [ActionList]";
		#endregion

		#region IActionListProvider Members
		public Core.Business.ActionList Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectActionList);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.ActionList actionList = new Core.Business.ActionList();

				if (!reader.IsDBNull(0)) actionList.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) actionList.ActionName = reader.GetString(1);

				return actionList;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.ActionList actionList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ActionName", SqlDbType.NVarChar, actionList.ActionName);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertActionList);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) actionList.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.ActionList actionList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ActionName", SqlDbType.NVarChar, actionList.ActionName);
			sql.AddParameter("@Id", SqlDbType.Int, actionList.Id);
			sql.ExecuteSql(SqlUpdateActionList);
		}

		public void Delete(Core.Business.ActionList actionList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, actionList.Id);
			sql.ExecuteSql(SqlDeleteActionList);
		}

		public IList<Core.Business.ActionList> GetAllActionList()
		{
			IList<Core.Business.ActionList> actionListlist = new List<Core.Business.ActionList>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllActionList);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.ActionList actionList = new Core.Business.ActionList();

					if (!reader.IsDBNull(0)) actionList.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) actionList.ActionName = reader.GetString(1);

					actionList.MarkOld();
					actionListlist.Add(actionList);
				}
				reader.Close();
			}
			return actionListlist;
		}
		#endregion
	}
}
